jetcrab\lexer\tokens/
punctuation.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
4pub enum Punctuation {
5    LeftParen,
6    RightParen,
7    LeftBrace,
8    RightBrace,
9    LeftBracket,
10    RightBracket,
11    Dot,
12    Semicolon,
13    Comma,
14    Colon,
15    Question,
16    Exclamation,
17    Tilde,
18    TemplateStart,
19    TemplateEnd,
20    TemplateExpr,
21}
22
23impl Punctuation {
24    pub fn as_str(&self) -> &'static str {
25        match self {
26            Punctuation::LeftParen => "(",
27            Punctuation::RightParen => ")",
28            Punctuation::LeftBrace => "{",
29            Punctuation::RightBrace => "}",
30            Punctuation::LeftBracket => "[",
31            Punctuation::RightBracket => "]",
32            Punctuation::Dot => ".",
33            Punctuation::Semicolon => ";",
34            Punctuation::Comma => ",",
35            Punctuation::Colon => ":",
36            Punctuation::Question => "?",
37            Punctuation::Exclamation => "!",
38            Punctuation::Tilde => "~",
39            Punctuation::TemplateStart => "${",
40            Punctuation::TemplateEnd => "}",
41            Punctuation::TemplateExpr => "${",
42        }
43    }
44
45    pub fn is_opening(&self) -> bool {
46        matches!(
47            self,
48            Punctuation::LeftParen | Punctuation::LeftBrace | Punctuation::LeftBracket
49        )
50    }
51
52    pub fn is_closing(&self) -> bool {
53        matches!(
54            self,
55            Punctuation::RightParen | Punctuation::RightBrace | Punctuation::RightBracket
56        )
57    }
58
59    pub fn matching_closing(&self) -> Option<Punctuation> {
60        match self {
61            Punctuation::LeftParen => Some(Punctuation::RightParen),
62            Punctuation::LeftBrace => Some(Punctuation::RightBrace),
63            Punctuation::LeftBracket => Some(Punctuation::RightBracket),
64            _ => None,
65        }
66    }
67
68    pub fn matching_opening(&self) -> Option<Punctuation> {
69        match self {
70            Punctuation::RightParen => Some(Punctuation::LeftParen),
71            Punctuation::RightBrace => Some(Punctuation::LeftBrace),
72            Punctuation::RightBracket => Some(Punctuation::LeftBracket),
73            _ => None,
74        }
75    }
76}